package localreader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

import java.util.List;

import lib.MyLog;

import org.apache.log4j.Logger;

import reference.Database;

public class UserMa {
	public UserMa(){
		Logger logger = MyLog.getLogger(UserMa.class.getName());
		
		this.UserFolder = new File(System.getProperty("user.dir")+File.separator+"local");
		if(this.UserFolder.isDirectory()==false){
			logger.error("MinerWeibo\\local does not exist.");
			return;
		}
		
		this.noUsers = new ArrayList<String>();
		this.abUsers = new ArrayList<String>();
		this.allFollowers = new ArrayList<String>();
		
	}
	
	private void readUser(File file, List<String> users){
		Logger logger = MyLog.getLogger(UserMa.class.getName());
		
		users.clear();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			String sCurrentLine = null;
			while((sCurrentLine=br.readLine())!=null){
				users.add(sCurrentLine);
			}
			if(br!=null)
			    br.close();
		} catch (FileNotFoundException e) {
			logger.error(file.getName()+"not find");
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	
	public List<String> getAllFol(){
		Logger logger = MyLog.getLogger(UserMa.class.getName());
		
		File f = new File(this.UserFolder, "Set1Follow.txt");
		if(f.exists()==false){
			logger.error("File does not exit, initialize ad user failed.");
			return null;
		}
		
		this.readUser(f, this.allFollowers);
		return this.allFollowers;
	}
	
	
	public List<String> getLocalUser() throws IOException{
		List<String> userList = new ArrayList<String>();
		userList.addAll(this.getLocalNor());
		userList.addAll(this.getLocalAbnor());
		return userList;
	}
	
	public List<String> getLocalNor() throws IOException{
		return this.getUser("allNormal.txt");
	}
	
	public List<String> getLocalAbnor() throws IOException{
		return this.getUser("allAbnormal.txt");
	
	}
	
	public List<String> reduction(List<String> userList){
		Collections.sort(userList);
		int size = userList.size();
		for(int i = 0; i < size - 1; i++){
			if(userList.get(i).equals(userList.get(i+1))){
				userList.remove(i+1);
				size--;
				i--;
			}
		}
		return userList;
	}

	public List<String> getUser(String file) throws IOException{
		File f = new File(this.UserFolder, file);
		if(f.exists()==false){
			System.out.println(file + " does not exit, initialize normalUsers failed.");
			return null;
		}
		
		BufferedReader br = new BufferedReader(new FileReader(f));	;
		List<String> userList = new LinkedList<String>();
		String str = null;
		while((str=br.readLine())!=null){
			userList.add(str);
		}
		br.close();
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));		
		List<String> List = this.reduction(userList);
		for(String uid : List){
			bw.write(uid+"\r\n");
		}
		bw.close();
		return  userList;
	}
	
	public List<String> getN1() throws IOException{
		return this.getUser("normal1.txt");
	}
	
	public List<String> getA1() throws IOException{
		return this.getUser("abnormal1.txt");
	}
	
	public void writeSet1Follow() throws IOException, SQLException{
		List<String> Set1 = this.getN1();
		Set1.addAll(this.getA1());
		Connection con = Database.connectSQL();
		List<String> Set1Follow = Database.getFollowByIds(Set1, con);
		Collections.sort(Set1Follow);
		
		File f = new File(this.UserFolder, "Set1Follow.txt");
		if(f.exists()==false){
			System.out.println("Set1Follow does not exit, initialize normalUsers failed.");
			return;
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));		
		for(String uid : Set1Follow){
			bw.write(uid+"\r\n");
		}
		bw.close();
		System.out.println("Write "+Set1Follow.size()+"  followers");
		
	}
	
	public void writeSet1Relation() throws IOException, SQLException{
		List<String> Set1 = this.getN1();
		Set1.addAll(this.getA1());
		Connection con = Database.connectSQL();
		List<String> Set1Relation = Database.getRelationByIds(Set1, con);
		List<String> Set1Reduction = reduction(Set1Relation);
		
		File f = new File(this.UserFolder, "Set1Relation.txt");
		if(f.exists()==false){
			System.out.println("Set1Follow does not exit, initialize normalUsers failed.");
			return;
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));		
		for(String uid : Set1Reduction){
			bw.write(uid+"\r\n");
		}
		bw.close();
		System.out.println("Write "+Set1Reduction.size()+"  followers");
		
	}
	
	public List<String> readSet1Relation() throws IOException{
		File f = new File(this.UserFolder, "Set1Relation.txt");
		if(f.exists()==false){
			System.out.println("Set1Follow does not exit, initialize normalUsers failed.");
			return null;
		}
		BufferedReader br = new BufferedReader(new FileReader(f));
		List<String> userList = new ArrayList<String>();
		String str = null;
		while((str=br.readLine())!=null){
			userList.add(str);
		}
		br.close();
		
		return userList;
		
	}
	
	public void updateSet1Relation() throws IOException, SQLException{
		List<String> userList = readSet1Relation();
		List<String> remainList = new ArrayList();
		Connection con = Database.connectSQL();
		for(String uid : userList){
			int num =Database.getStatusNum(uid, con);
			System.out.println(uid+" "+num);
			if(num < 5){
				remainList.add(uid);
			}
				
		}
		con.close();
		
		File f = new File(this.UserFolder, "Set1Relation.txt");
		BufferedWriter br = new BufferedWriter(new FileWriter(f));
		for(String uid : remainList){
			br.write(uid+"\r\n");
		}
		br.close();
	}
	
	public List<String> getSet1Follow() throws IOException{
		return this.getUser("Set1Follow");
	}
	
	public List<String> getSet1() throws IOException{
		List<String> users = this.getN1();
		users.addAll(this.getA1());
		return users;
	}
	private File UserFolder;
	
	private List<String> noUsers;
	private List<String> abUsers;
	private List<String> allFollowers;
}
